package com.micro.school.edu.service.impl;

import java.util.List;

import com.alibaba.druid.util.StringUtils;
import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.utils.DateUtils;
import com.micro.school.common.core.utils.SecurityContextHolderUtils;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.common.security.utils.SecurityUtils;
import com.micro.school.edu.convert.CollegeMajorConvert;
import com.micro.school.edu.domain.CollegeMajor;
import com.micro.school.edu.domain.vo.CollegeMajorVo;
import com.micro.school.edu.domain.vo.SchoolCollegeVo;
import com.micro.school.edu.mapper.CollegeMajorMapper;
import com.micro.school.edu.mapper.SchoolCollegeMapper;
import com.micro.school.edu.service.ICollegeMajorService;

import com.micro.school.system.api.RemoteOrderService;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.vo.CollegeMajorVos;
import com.micro.school.system.api.domain.vo.StudentInfoVos;
import com.micro.school.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 学院专业Service业务层处理
 *
 * @author rakic
 * @date 2025-08-07
 */
@Service
public class CollegeMajorServiceImpl implements ICollegeMajorService
{
    @Autowired
    private CollegeMajorMapper collegeMajorMapper;

    @Autowired
    private SchoolCollegeMapper schoolCollegeMapper;

    @Autowired
    private RemoteUserService remoteUserService;


    @Autowired
    private RemoteOrderService remoteOrderService;

    /**
     * 查询学院专业
     *
     * @param id 学院专业主键
     * @return 学院专业
     */
    @Override
    public CollegeMajorVo selectCollegeMajorById(Long id)
    {
        return collegeMajorMapper.selectCollegeMajorById(id);
    }

    @Override
    public CollegeMajorVos selectCollegeMajorByIds(Long id)
    {
        return collegeMajorMapper.selectCollegeMajorByIds(id,SecurityContextHolder.getUserTenantId());
    }

    /**
     * 查询学院专业列表
     *
     * @param collegeMajorVo 学院专业
     * @return 学院专业
     */
    @Override
    public List<CollegeMajorVo> selectCollegeMajorList(CollegeMajorVo collegeMajorVo)
    {
        collegeMajorVo.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            if(collegeMajorVo.getCollegeIds() == null && collegeMajorVo.getCollegeId() == null){
                collegeMajorVo.setCollegeIds(StringUtils.isEmpty(SecurityContextHolder.getUserCollegeId()) ? null: SecurityContextHolder.getUserCollegeId());
            }
            if (collegeMajorVo.getCampusIds() == null && collegeMajorVo.getCampusId() == null) {
                collegeMajorVo.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId() : null);
            }
        }
        return collegeMajorMapper.selectCollegeMajorList(collegeMajorVo);
    }

    /**
     * 新增学院专业
     *
     * @param collegeMajorVo 学院专业
     * @return 结果
     */
    @Override
    public AjaxResult insertCollegeMajor(CollegeMajorVo collegeMajorVo)
    {
        CollegeMajorVos collegeMajorVos = new CollegeMajorVos();
        collegeMajorVos.setMajorName(collegeMajorVo.getMajorName());
        collegeMajorVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        collegeMajorVos.setDeleteFlag("0");
        collegeMajorVos.setCollegeId(collegeMajorVo.getCollegeId());
        collegeMajorVos.setModelId(collegeMajorVo.getModelId());
        List<CollegeMajorVos> collegeMajorVos1 = collegeMajorMapper.selectCollegeMajorLists(collegeMajorVos);
        if (collegeMajorVos1 != null && collegeMajorVos1.size() > 0){
            return AjaxResult.error("插入失败，专业名称不能重复");
        }else{
            CollegeMajor collegeMajor = CollegeMajorConvert.INSTANCE.convert(collegeMajorVo);

            collegeMajor.setCreateTime(DateUtils.getNowDate());

            SchoolCollegeVo schoolCollegeVo = schoolCollegeMapper.selectSchoolCollegeById(collegeMajorVo.getCollegeId());
            collegeMajor.setCampusId(schoolCollegeVo.getCampusId());
            collegeMajor.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
            StudentInfoVos studentInfoVos = remoteUserService.selectSchoolById(Long.valueOf(SecurityContextHolder.getUserTenantId()));
            collegeMajor.setSchoolName(studentInfoVos.getSchoolName());
            collegeMajorMapper.insertCollegeMajor(collegeMajor);
            return AjaxResult.success("插入成功");
        }




    }

    /**
     * 修改学院专业
     *
     * @param collegeMajorVo 学院专业
     * @return 结果
     */
    @Override
    public int updateCollegeMajor(CollegeMajorVo collegeMajorVo)
    {
        CollegeMajor collegeMajor = CollegeMajorConvert.INSTANCE.convert(collegeMajorVo);
        collegeMajor.setUpdateTime(DateUtils.getNowDate());
        CollegeMajorVo collegeMajorVo1 = collegeMajorMapper.selectCollegeMajorById(collegeMajorVo.getId());
        if (!collegeMajorVo.getMajorName().equals(collegeMajorVo1.getMajorName())){
            remoteUserService.updateAllCollegeMajor(collegeMajorVo.getMajorName(),collegeMajorVo.getId(),collegeMajorVo1.getCollegeName());
            remoteOrderService.updateAllCollegeMajor(collegeMajorVo.getMajorName(),collegeMajorVo.getId());
        }
        collegeMajorMapper.updateCollegeMajor(collegeMajor);

        return 1;
    }

    @Override
    public List<CollegeMajorVos> selectCollegeMajorLists(CollegeMajorVos collegeMajorVos) {
        collegeMajorVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            collegeMajorVos.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId(): null);
            collegeMajorVos.setCollegeIds(StringUtils.isEmpty(SecurityContextHolder.getUserCollegeId()) ? null: SecurityContextHolder.getUserCollegeId());
        }
        return collegeMajorMapper.selectCollegeMajorLists(collegeMajorVos);
    }

    @Override
    public List<CollegeMajorVos> selectCollegeMajorLists1(CollegeMajorVos collegeMajorVos) {
        return collegeMajorMapper.selectCollegeMajorLists(collegeMajorVos);
    }

    /**
     * 批量删除学院专业
     *
     * @param ids 需要删除的学院专业主键
     * @return 结果
     */
//    @Override
//    public int deleteCollegeMajorByIds(List<Long> ids)
//    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        return collegeMajorMapper.deleteBatchIds(ids);
//    }

    /**
     * 删除学院专业信息
     *
     * @param id 学院专业主键
     * @return 结果
     */
//    @Override
//    public int deleteCollegeMajorById(Long id)
//    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        return collegeMajorMapper.deleteById(id);
//    }

}
